package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private AttrValueService attrValueService;

    @Autowired
    private FacilityInfoService facilityInfoService;

    @Autowired
    private LabelInfoService labelInfoService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;

    @Autowired
    private RoomFacilityService RoomFacilityService;

    @Autowired
    private RoomLabelService RoomLabelService;

    @Autowired
    private RoomAttrValueService RoomAttrValueService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    PaymentTypeMapper paymentTypeMapper;

    @Autowired
    LeaseTermMapper leaseTermMapper;

    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    private RoomAttrValueMapper RoomAttrValueMapper;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymenTypeMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;



    //分页查询房间信息
    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);
    }

    //新增或修改房间信息
    @Transactional
    @Override
    public void saveOrUpdateRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        boolean isNull = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
        if (isNull) {
            //删除原有数据
            LambdaQueryWrapper<GraphInfo> graphWrapper = new LambdaQueryWrapper<>();
            graphWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphWrapper);

            QueryWrapper<RoomAttrValue> attrValueWrapper = new QueryWrapper<>();
            attrValueWrapper.eq("room_id", roomSubmitVo.getId());
            RoomAttrValueMapper.delete(attrValueWrapper);


            QueryWrapper<RoomFacility> facilityWrapper = new QueryWrapper<>();
            facilityWrapper.eq("room_id", roomSubmitVo.getId());
            roomFacilityMapper.delete(facilityWrapper);

            QueryWrapper<RoomLabel> labelWrapper = new QueryWrapper<>();
            labelWrapper.eq("room_id", roomSubmitVo.getId());
            roomLabelMapper.delete(labelWrapper);

            QueryWrapper<RoomPaymentType> paymentWrapper = new QueryWrapper<>();
            paymentWrapper.eq("room_id", roomSubmitVo.getId());
            roomPaymenTypeMapper.delete(paymentWrapper);

            QueryWrapper<RoomLeaseTerm> leaseWrapper = new QueryWrapper<>();
            leaseWrapper.eq("room_id", roomSubmitVo.getId());
            roomLeaseTermMapper.delete(leaseWrapper);

        }
        //1.保存新的graphInfoList
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //2.保存新的roomAttrValueList
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
           RoomAttrValueService.saveBatch(roomAttrValueList);
        }

        //3.保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            RoomFacilityService.saveBatch(roomFacilityList);
        }

        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            RoomLabelService.saveBatch(roomLabelList);
        }

        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }

    }

    //根据房间id查询房间详情
    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //1.基础房间信息
        RoomInfo roomInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        //2.公寓名字以及公寓位置
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //3.图片列表
        QueryWrapper<GraphInfo> graphWrapper = new QueryWrapper<>();
        graphWrapper.eq("item_id", id).eq("item_type", ItemType.ROOM);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphWrapper);
        List<GraphVo> graphVoList = graphInfoList.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());
        // 配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

        // 标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

        // 支付方式
        List<PaymentType> paymentTypes = paymentTypeMapper.selectListByRoomId(id);

        // 租期
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectListByRoomId(id);

        // 基本属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setLeaseTermList(leaseTerms);

        return roomDetailVo;
    }

    @Transactional
    @Override
    public void removeRoomById(Long id) {
        //1.删除房间信息
        baseMapper.deleteById(id);
        //2.删除GraphInfo
        QueryWrapper<GraphInfo> graphWrapper = new QueryWrapper<>();
        graphWrapper.eq("item_id", id).eq("item_type", ItemType.ROOM);
        graphInfoService.remove(graphWrapper);
        //3.删除RoomAttrValue
        QueryWrapper<RoomAttrValue> roomAttrValueWrapper = new QueryWrapper<>();
        roomAttrValueWrapper.eq("room_id", id);
        RoomAttrValueMapper.delete(roomAttrValueWrapper);
        //4.删除RoomFacility
        QueryWrapper<RoomFacility> roomFacilityWrapper = new QueryWrapper<>();
        roomFacilityWrapper.eq("room_id", id);
        roomFacilityMapper.delete(roomFacilityWrapper);
        //5.删除RoomLabel
        QueryWrapper<RoomLabel> roomLabelWrapper = new QueryWrapper<>();
        roomLabelWrapper.eq("room_id", id);
        roomLabelMapper.delete(roomLabelWrapper);
        //6.删除RoomPaymentType
        QueryWrapper<RoomPaymentType> roomPaymentTypeWrapper = new QueryWrapper<>();
        roomPaymentTypeWrapper.eq("room_id", id);
        roomPaymenTypeMapper.delete(roomPaymentTypeWrapper);
        //7.删除RoomLeaseTerm
        QueryWrapper<RoomLeaseTerm> roomLeaseTermWrapper = new QueryWrapper<>();
        roomLeaseTermWrapper.eq("room_id", id);
        roomLeaseTermMapper.delete(roomLeaseTermWrapper);

    }

    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        roomInfoMapper.updateReleaseStatusById(id, status);
    }

    @Override
    public List<RoomInfo> listBasicByApartmentId(Long id) {
        return roomInfoMapper.listBasicByApartmentId(id);
    }

}




